<template>
<div>

  <div class="wrapper">
    <p class="holder">
      <a @click="toRecord" href="#" class="animBtn themeC">记录账单</a><br>
      <a @click="toView" href="#" class="animBtn themeD">查看账单</a>
    </p>
  </div>
</div>
</template>

<script>
import Vue from "vue";

export default {
  name: "MainPage",
  methods:{
    toRecord:function () {
      setTimeout(() =>{
        this.$router.push({path:'/record'});
      },300)

    },
    toView:function () {
      setTimeout(() =>{
        this.$router.push({path:'/view'})
      },300)

    },
    setViewCookie:function (cname,cvalue) {
        // var cname='familyvuegitee_background';
        var exdays=20;
        // cvalue = 'https://cn.bing.com/'+cvalue;
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        // console.info(cname + "=" + cvalue + "; " + expires);
        document.cookie = cname + "=" + cvalue + "; " + expires;
        // console.info("设置cookie成功："+document.cookie);
        // alert("设置cookie成功："+cvalue)

    },
    getCookie : function (cname) {
      var name = cname + "=";
      var ca = document.cookie.split(';');
      // console.log("获取cookie,现在循环")
      for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        // console.log(c)
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) != -1){
          return c.substring(name.length, c.length);
        }
      }
      return "";
    },
    getDate:function (format,type) {
      var d=new Date();
      var t =0;
      var day=0;

      if (format=='yesterday') {
          t = d.getTime() - 1000 * 60 * 60 * 24;
          day = new Date(t);
        return( day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate());
      } else if (format == 'week'){
        let day1 = d.getDay();
        day=new Date();
        if (day1 > 1){
          t = d.getTime() - 1000 * 60 * 60 * 24* (day1-1);
          day = new Date(t);
        }else if (day1 === 0){
          t = d.getTime() - 1000 * 60 * 60 * 24* 6;
          day = new Date(t);
        }
        return( day.getFullYear()+'-'+(day.getMonth() + 1)+ '-' + day.getDate() );
      }else if (format=='month'){
        return( d.getFullYear()+'-'+(d.getMonth() + 1) );
      }else if (format=='season'){
        let number = d.getMonth() + 1;
        var s= Math.ceil(number/3);
        return( d.getFullYear()+'-'+ s) ;
      }else if (format=='year'){
        return ( d.getFullYear())
      }else if (type=="day"){
        // format为 -1返回昨天 -2返回前天 -3返回上3天 -4.。。。
          t = d.getTime() + 1000 * 60 * 60 * 24* format;
          day = new Date(t);
        return( day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate());

      }else if (type=="month"){
        // format为 -1返回上月 -2上二个月。。。最多减去12

        let number1 = d.getMonth()+1;
        let number2 = d.getFullYear();

        if (number1+format <1){
          format = (number1+format)
          number2 = --number2;
          return number2+'-'+(12+format);
        }else {
          return number2 + '-' + (number1 + format);
        }
      }


    },
    getListVById:function (list,id) {
      for (let i = 0; i < list.length; i++) {
        let element = list[i];
        if (element.time ==id){
          return element.value;
        }
      }
    }
  },
  data(){
    return{
      v_main:0,
      viewCookies1:Vue.prototype.viewCookies1,
      viewCookies2:Vue.prototype.viewCookies2
    }
  },
  created() {
    //首先查询有没有cookie 如果没有就设置
    for (let i = 0; i < this.viewCookies1.length; i++) {
      let viewCookie = this.viewCookies1[i]; //获取每个cookieKey
      let cookieV = this.getCookie(viewCookie); //获取浏览器存储的cookieValue
      let currentDate = this.getDate(viewCookie);
      if (cookieV == ""){
        //找不到就新建
        this.setViewCookie(viewCookie,JSON.stringify({time: currentDate , value:0}))
      }else {
        //找到了，判断一下是否需要重置
        if (JSON.parse(cookieV).time != currentDate){
          this.setViewCookie(viewCookie,JSON.stringify({time: currentDate , value:0}))
        }
      }
    }
    for (let i = 0; i < this.viewCookies2.length; i++) {
      let viewCookie2 = this.viewCookies2[i]; //获取每个cookieKey
      let cookie2V = this.getCookie(viewCookie2); //获取浏览器存储的cookieValue
      var v=[];
      let b = i===0;
      if (b){ //给v赋值前7天
        for (let j = -1; j >=-7 ; j--) {

          v.push({time:this.getDate(j,'day'), value:0})
        }
      }else { //给v赋值前6月
        for (let j = -0; j >=-5 ; j--) {
          v.push({time:this.getDate(j,'month'), value:0})
        }
      }

      if (cookie2V == ""){
          this.setViewCookie(viewCookie2,JSON.stringify(v))
      }else if (b){
        //清空过去数据
        let cookie2Day = JSON.parse(this.getCookie(viewCookie2));
        for (let j = 0; j < v.length; j++) {
          let vTime = v[j].time;
          let cValue = this.getListVById(cookie2Day,vTime);
          if (cValue=='' || cValue==undefined){
            cValue = 0;
          }
          v[j].value = cValue;
        }
        this.setViewCookie(viewCookie2,JSON.stringify(v))

      }else {
        //清空过去数据
        let cookie2Month = JSON.parse(this.getCookie(viewCookie2));
        for (let j = 0; j < v.length; j++) {
          let vTime = v[j].time;
          let cValue = this.getListVById(cookie2Month,vTime);
          if (cValue=='' || cValue==undefined){
            cValue = 0;
          }
          v[j].value = cValue;
        }
        this.setViewCookie(viewCookie2,JSON.stringify(v))
      }
    }
  }
}
</script>

<style scoped>

</style>
